﻿using System;
using System.Collections.Generic;
using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for ReturnView.xaml
    /// </summary>
    public partial class ReturnView
    {
        private readonly OrderDetailController _orderDetailController;
        private readonly OrderDetailStatusController _orderDetailStatusController;
        private readonly ComicController _comicController;
        private readonly ComicStatusController _comicStatusController;

        private IList<OrderDetail> _orderDetails;
        private IList<OrderDetailData> _orderDetailDatas;
        private readonly IList<OrderDetailStatus> _orderDetailStatuses;
        private readonly IList<ComicStatus> _comicStatuses;

        private readonly Order _order;

        public ReturnView(Order order)
        {
            InitializeComponent();

            _order = order;

            _orderDetailController = new OrderDetailController();
            _orderDetailStatusController = new OrderDetailStatusController();
            _comicController = new ComicController();
            _comicStatusController = new ComicStatusController();

            _orderDetailStatuses = _orderDetailStatusController.GetAllOrderDetailStatus();
            _comicStatuses = _comicStatusController.GetAllComicStatus();

            _orderDetails = _orderDetailController.GetOrderDetailByOrder(_order);
            _orderDetailDatas = _orderDetailController.GetAllOrderDetailDataByOrder(_order);
            dataGridOrderDetail.ItemsSource = _orderDetailDatas;
        }

        private void RefreshGrid()
        {
            dataGridOrderDetail.ItemsSource = null;
            _orderDetails = _orderDetailController.GetOrderDetailByOrder(_order);
            _orderDetailDatas = _orderDetailController.GetAllOrderDetailDataByOrder(_order);
            dataGridOrderDetail.ItemsSource = _orderDetailDatas;
        }

        private void ButtonReturnedClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridOrderDetail.SelectedItem == null)
                    throw new NullReferenceException();

                var oldOrderDetail = _orderDetails[dataGridOrderDetail.SelectedIndex];

                var newOrderDetail = new OrderDetail
                                         {
                                             Id = oldOrderDetail.Id,
                                             Order = oldOrderDetail.Order,
                                             Comic = oldOrderDetail.Comic,
                                             OrderDetailStatus = _orderDetailStatuses[1]
                                         };

                _orderDetailController.EditOrderDetail(newOrderDetail);
                RefreshGrid();

                var oldComic = _comicController.GetComicById(oldOrderDetail.Comic.Id);

                var newComic = new Comic
                                   {
                                       Id = oldComic.Id,
                                       Title = oldComic.Title,
                                       Author = oldComic.Author,
                                       Publisher = oldComic.Publisher,
                                       Year = oldComic.Year,
                                       Rent = oldComic.Rent,
                                       ComicStatus = _comicStatuses[0],
                                       Image = oldComic.Image
                                   };

                _comicController.EditComic(newComic);
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("pilih yang mau diedit");
            }
        }

        private void ButtonUnreturnedClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridOrderDetail.SelectedItem == null)
                    throw new NullReferenceException();

                var oldOrderDetail = _orderDetails[dataGridOrderDetail.SelectedIndex];

                var newOrderDetail = new OrderDetail
                                         {
                                             Id = oldOrderDetail.Id,
                                             Order = oldOrderDetail.Order,
                                             Comic = oldOrderDetail.Comic,
                                             OrderDetailStatus = _orderDetailStatuses[0]
                                         };

                _orderDetailController.EditOrderDetail(newOrderDetail);
                RefreshGrid();

                var oldComic = _comicController.GetComicById(oldOrderDetail.Comic.Id);

                var newComic = new Comic
                                   {
                                       Id = oldComic.Id,
                                       Title = oldComic.Title,
                                       Author = oldComic.Author,
                                       Publisher = oldComic.Publisher,
                                       Year = oldComic.Year,
                                       Rent = oldComic.Rent,
                                       ComicStatus = _comicStatuses[1],
                                       Image = oldComic.Image
                                   };

                _comicController.EditComic(newComic);
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("pilih yang mau diedit");
            }
        }
    }
}
